package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.*;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    // 插入订单数据
    void insert(Orders orders);

    // 批量插入订单详情数据
    void insertOrderDetail(List<OrderDetail> orderDetailList);

    // 分页查询
    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    // 条件查询
    Page<OrderVO> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);

    // 查询订单详情
    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> getOrderDetailByOrderId(Long id);

    // 根据id查询订单数据
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    // 根据订单号查询订单
    @Select("select id from orders where number = #{number}")
    Long getIdByNumber(String number);

    // 统计订单数量
    @Select("select count(id) from orders where status = #{status}")
    Integer countByStatus(Integer status);

    // 取消订单
    @Update("update orders set status = 6, cancel_reason = #{ordersCancelDTO.cancelReason}, cancel_time = #{cancelTime} where id = #{ordersCancelDTO.id}")
    void cancel(OrdersCancelDTO ordersCancelDTO, LocalDateTime cancelTime);

    // 拒绝订单
    @Update("update orders set status = 6, rejection_reason = #{rejectionReason} where id = #{id}")
    void rejection(OrdersRejectionDTO ordersRejectionDTO);

    // 确认订单
    @Update("update orders set status = 3 where id = #{id}")
    void confirm(OrdersConfirmDTO ordersConfirmDTO);

    // 派送订单
    @Update("update orders set status = 4 where id = #{id}")
    void delivery(Long id);

    // 完成订单
    @Update("update orders set status = 5, checkout_time = now() where id = #{id}")
    void complete(Long id);

    // 订单支付
    @Update("update orders set pay_method = #{payMethod}, pay_status = 1, status = 2 where number = #{orderNumber}")
    void pay(OrdersPaymentDTO ordersPaymentDTO);

    // 查询订单超时未支付
    @Select("select * from orders where status = 1 and order_time < #{time}")
    List<Orders> getTimeoutOrders(LocalDateTime time);

    // 取消订单超时未支付
    @Update("update orders set status = 6, cancel_reason = #{cancelReason}, cancel_time = now() where id = #{id}")
    void cancelByTimeOut(Orders orders);

    // 查询自动接单的订单
    @Select("select * from orders where status = 2 and order_time < #{time}")
    List<Orders> getAutoConfirmOrders(LocalDateTime time);
    // 自动接单
    @Update("update orders set status = 3 where id = #{id}")
    void autoConfirm(Orders orders);

    //查询派送中的订单
    @Select("select * from orders where status = 4 and order_time < #{time}")
    List<Orders> getDeliveryOrders(LocalDateTime time);

    // 完成订单
    void completeByDelivery(List<Orders> ordersList);

}
